/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.phoenix.repository.dao.mysql;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.phoenix.repository.SQLParameter;
import net.phoenix.repository.dao.ValueTableSearchDao;

import com.sun.rowset.CachedRowSetImpl;
/**
 * Jigsaw server
 * 
 * @author    <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date      2012-11-24
 * @version   1.0.0
 */
@SuppressWarnings("restriction")
public class MysqlValueTableSearchDao extends ValueTableSearchDao {

	/* (non-Javadoc)
	 * @see net.phoenix.repository.dao.ValueTableSearchDao#search(java.lang.String, net.phoenix.repository.SQLParameter[], long, long)
	 */
	@Override
	public ResultSet search(String query, SQLParameter[] parameters, long fromIndex, long number) throws SQLException {
		String sql = toMysqlRangeQuery(query);
		PreparedStatement statement = null;
		try {
			List<SQLParameter> all = new ArrayList<SQLParameter>();
			if (parameters != null)
				Collections.addAll(all, parameters);
			all.add(new SQLParameter(Types.BIGINT, new Long(fromIndex )));
			all.add(new SQLParameter(Types.BIGINT, new Long(number)));
			statement = this.createStatement(sql, (SQLParameter[]) all.toArray(new SQLParameter[all.size()]));
			ResultSet rs = statement.executeQuery();
			CachedRowSetImpl cache= new CachedRowSetImpl();
			cache.populate(rs);
			return cache;
		} finally {
			this.close(statement);
		}
	}

	@Override
	public int count(String query, SQLParameter[] parameters, long fromIndex, long number) throws SQLException {
		String sql = "select count(*) from (" +toMysqlRangeQuery(query) +") tmp";
		PreparedStatement statement = null;
		try {
			List<SQLParameter> all = new ArrayList<SQLParameter>();
			if (parameters != null)
				Collections.addAll(all, parameters);
			all.add(new SQLParameter(Types.BIGINT, new Long(fromIndex )));
			all.add(new SQLParameter(Types.BIGINT, new Long(number)));
			statement = this.createStatement(sql, (SQLParameter[]) all.toArray(new SQLParameter[all.size()]));
			ResultSet rs = statement.executeQuery();
			int result = rs.getInt(1);
			rs.close();
			return result;
		} finally {
			this.close(statement);
		}
	}
	

}
